<?php
	require('Header_iframe.php');
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/livevalidate.css" />
<script type="text/javascript" src="js/livevalidation.js"></script>
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript">
var ajax = new Array();
var current_dept;
var current_number;
var selectCurrentNum;
var network_id = <?php echo $fb_network_id; ?>;
var fb_user_id = <?php echo $fb_user_id; ?>;
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function sendRemoveCourse(course_id) {
    var ulink=document.getElementById("ulink").value;
    new Ajax.Request("ajax_requests/removeCourse.php", 
    { 
           method: 'post', 
           postBody: 'user_id=' + fb_user_id + '&course_id='+ course_id + '&ulink=' + ulink,
           onComplete: refreshPage 
    });
}
function refreshPage(){
    window.location.reload();
}
</script>
<script type="text/javascript" src="prototype_js/prototype-1.6.0.3.js"></script>
<script type="text/javascript">
function sendSubmitULINK() {
    new Ajax.Request("ajax_requests/addULink.php", 
    { 
           method: 'post', 
           postBody: 'ulinkID='+ $F('ulinkID')+ '&studentID='+$F('studentID')+'&user_id='+$F('facebook_user_id') ,
           onComplete: showULinkResponse 
    });
}

function showULinkResponse(req){
    $('showULINK').innerHTML= req.responseText;
}

function sendRequestCourse() {
    new Ajax.Request("ajax_requests/sendRequestCourse.php", 
    { 
           method: 'post', 
           postBody: 'courseDept='+ $F('courseDept')+ '&courseNum='+$F('courseNum') +'&user_first='+$F('user_first') +'&user_last='+$F('user_last') +'&ulink_id='+$F('ulink_id'),
           onComplete: showRequestCourse 
    });
}

function showRequestCourse(req){
    $('showRequestCourse').innerHTML= req.responseText; 
}
</script>
</head>

<body class="small">
<br>
<table cellspacing=20> 
<tr>
<td>
<img src="pics/reachhand2_100.gif" />
</td>
<td> Hello <?php echo $_SESSION['fb_user_firstname'];?>, welcome to Study Buddies! Study Buddies is a tool provided by Reach to help students interact with classmates and tutors, and to enhance learning in college courses at the University of Louisville. 
</td>
</tr>
</table>


<!-- We don't have user ulink or student id yet, show this form -->

<?php if($reach_veri ==0): ?>
<table id="add_ulink" cellspacing="20"  width=100% class="small" align="left">
<form id="ulinkform" onsubmit="return false;" method="post">
<tr>
<td align="left">
 &nbsp;&nbsp;&nbsp;For starters, we'll need your ULink ID and Student ID:
</td>
</tr>
<tr>
    <td align="left">
    &nbsp;&nbsp;&nbsp;ULink ID:
     &nbsp;&nbsp;&nbsp;<input type="text" id="ulinkID" name="ulinkID"> 
    <script type ="text/javascript">
    var f1 = new LiveValidation("ulinkID", { validMessage: " Good!", wait: 500 } );
    f1.add( Validate.Presence, 
                  { failureMessage: " Can't be empty!" } );
    f1.add( Validate.Presence );
    f1.add( Validate.Length, {minimum:7, maximum: 10});
    f1.add( Validate.Length );
    </script>
</tr>
<tr>
    <td align="left">
    &nbsp;&nbsp;&nbsp;Student ID:
    <input type="text" id="studentID" name="studentID"> 
    <script type ="text/javascript">
    var f1 = new LiveValidation("studentID", { validMessage: " Good!", wait: 500 } );
    f1.add( Validate.Presence, 
                  { failureMessage: " Can't be empty!" } );
    f1.add( Validate.Presence );
    f1.add( Validate.Length, {minimum:6, maximum: 10});
    f1.add( Validate.Length );
    f1.add( Validate.Numericality, { onlyInteger: true } );
    </script>
    </td>
</tr>

    <input type="hidden" id="facebook_user_id" name="facebook_user_id" value="<?php echo $fb_user_id;?>" />
<tr><td>
    &nbsp;&nbsp;&nbsp;<input type="submit" value="submit" onClick="sendSubmitULINK()" >
</td></tr>
</form>
<tr><td>
 &nbsp;&nbsp;&nbsp;<div class="small" id="showULINK"></div>
</tr></td>
</table>
<?php endif;?>







<!-- We have ulink and student id , show this form -->
<?php if($reach_veri ==1): ?>
    
<?php 
  /* try to validate user courses here*/
  $sql = "SELECT ulink_id from fb_users where user_id='$fb_user_id'";
  $result=$db->Execute($sql);
  $ulink= $result->fields['ulink_id'];
  if(!$ulink) { echo "<h1><b> Error: There was a problem locating your ULink id. Please contact Reach to have this reset. Sorry :( </b></h1>"; exit;}


  $sql2 = "select course_id from fb_reach_student_courses where student_ulink='$ulink'";
  $result2 = $db->Execute($sql2);
  //reach_student_courses -> user_courses
  while(!$result2->EOF){
    $curr_id=$result2->fields['course_id'];
    $sql3= "insert into fb_user_courses values('$fb_user_id','$curr_id')";
    $db->Execute($sql3);
    $result2->MoveNext();
  }

?>
<table id="show_courses" class="small" cellspacing="20">
<form id="dropcourse" onsubmit="return false;" method="post">
    <input type="hidden" id="ulink" name="ulink" value="<?php echo $ulink;?>" />
<tr>
<th align="left">Current Reach Courses:</th>
</tr>
<?php
//see if the user has any courses
$sql="select course_id from fb_user_courses where user_id='$fb_user_id'";
$result=$db->Execute($sql);
$num_rec= $result->RecordCount();

if($reach_veri==1 && $num_rec > 0){
while(!$result ->EOF){
    for($i=0,$max=$result->FieldCount();$i< $max;$i++){
        $course_id =(int) $result->fields['course_id'];
        //get name and display
        $sql2="select course_dept, course_num, course_name from fb_courses where id=$course_id";
        $result2 = $db->Execute($sql2);
        echo "<tr><td>" . $result2->fields['course_dept'] . "</td><td>" . $result2->fields['course_num'] . "</td><td>" . $result2->fields['course_name'] . "</td>";
        echo "<td><input type=\"button\" name=\"$course_id\" value=\"Drop\" onClick=\"sendRemoveCourse('$course_id');\"></td></tr>";
        //$string =  "<tr><td>" . $result2->fields['course_dept'] . "</td><td>" . $result2->fields['course_num'] . "</td><td>" . $result2->fields['course_name'] . "</td></tr>";
    }
    $result->MoveNext();
}
}
//we have ulink/student id, but not courses
else{
    echo "<tr><td> Study Buddies hasn't enrolled you in any courses, please request them below. </td</tr>"; 
}
?>
</form>
</table>

<!--request course form-->
<form id="request" onsubmit="return false;" method="post">
<table cellspacing="20"  width=100% class="small" align="left">
<tr>
<th align="left">
Request more courses:
</th>
</tr>
<tr>
    <td align="left">
    Course Dept:
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" id="courseDept" name="courseDept"> 
    <script type ="text/javascript">
    var f1 = new LiveValidation("courseDept", { validMessage: " Good!", wait: 500 } );
    f1.add( Validate.Presence, 
                  { failureMessage: " Can't be empty! example: ANTH" } );
    f1.add( Validate.Presence );
    f1.add( Validate.Length, {minimum:2, maximum: 6});
    f1.add( Validate.Length );
    </script>
</tr>
<tr>
    <td align="left">
    Course Number:
    &nbsp;&nbsp;&nbsp;<input type="text" id="courseNum" name="courseNum"> 
    <script type ="text/javascript">
    var f1 = new LiveValidation("courseNum", { validMessage: " Good!", wait: 500 } );
    f1.add( Validate.Presence, 
                  { failureMessage: " Can't be empty! example: 201" } );
    f1.add( Validate.Presence );
    f1.add( Validate.Length, {minimum:3, maximum: 5});
    f1.add( Validate.Length );
    f1.add( Validate.Numericality, { onlyInteger: true } );
    </script>
    </td>
</tr>

    <input type="hidden" id="user_first" name="user_first" value="<?php echo $fb_first_name;?>" />
    <input type="hidden" id="user_last" name="user_last" value="<?php echo $fb_last_name;?>" />
    <input type="hidden" id="ulink_id" name="ulink_id" value="<?php echo $ulink;?>" />
<tr><td>
    <input type="submit" value="submit" onClick="sendRequestCourse()" >
</td></tr>
<tr><td>
 <b> <div class="small" id="showRequestCourse"></div> </b>
</td></tr>
</table>
</form>
<?php endif;?>
</body>
</html>
